<template>
  <div class="log-view">
    <h2>科室操作日志</h2>
    
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      style="width: 100%; margin-top: 20px;"
      border
    >
      <el-table-column prop="id" label="序号" width="80" />
     
      <el-table-column prop="creator" label="创建人" width="120" />
      <el-table-column prop="updator" label="更新人" width="120" />
       <el-table-column prop="logContent" label="日志内容" min-width="300" />
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #default="scope">
          {{ formatDateTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
        <template #default="scope">
          {{ formatDateTime(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="isDeleted" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.isDeleted ? 'danger' : 'success'">
            {{ scope.row.isDeleted ? '已删除' : '正常' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-wrapper">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="total"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
        :hide-on-single-page="false"
        :pager-count="5"
        :page-sizes="[10, 20, 50, 100]"
        style="margin-top: 16px;"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import moment from 'moment'
import { axiosrmedical } from '@/http/axiosRead'

// 表格相关
const tableData = ref<any[]>([])
const tableLoading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 格式化日期时间
const formatDateTime = (dateStr: string) => {
  if (!dateStr) return '-'
  return moment(dateStr).format('YYYY-MM-DD HH:mm:ss')
}

// 获取日志数据
const fetchLogData = async () => {
  tableLoading.value = true
  try {
    const res = await axiosrmedical.get('/Department/Log', {
      params: {
        pageNum: pageNum.value,
        pageSize: pageSize.value
      }
    })
    
    if (res.data.code === 200) {
      tableData.value = res.data.pageData || []
      total.value = res.data.totaCount || 0
    } else {
      ElMessage.error(res.data.msg || '获取日志数据失败')
      tableData.value = []
      total.value = 0
    }
  } catch (e) {
    ElMessage.error('获取日志数据失败')
    tableData.value = []
    total.value = 0
  } finally {
    tableLoading.value = false
  }
}

// 分页处理
const handlePageChange = (val: number) => {
  pageNum.value = val
  fetchLogData()
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  pageNum.value = 1
  fetchLogData()
}

// 页面加载时获取数据
onMounted(() => {
  fetchLogData()
})
</script>

<style scoped>
.log-view {
  padding: 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.pagination-wrapper {
  display: flex;
  justify-content: flex-end;
}

h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 20px;
}
</style>

